package com.school.empsys.controller;

import com.school.empsys.constants.ResponseState;
import com.school.empsys.dto.ResponseInfo;
import com.school.empsys.model.EmpAttendance;
import com.school.empsys.model.EmpLeavePermit;
import com.school.empsys.page.table.PageTableHandler;
import com.school.empsys.page.table.PageTableHandler.CountHandler;
import com.school.empsys.page.table.PageTableHandler.ListHandler;
import com.school.empsys.page.table.PageTableRequest;
import com.school.empsys.page.table.PageTableResponse;
import com.school.empsys.service.EmpAttendanceService;
import com.school.empsys.service.EmpLeavePermitService;
import com.school.empsys.utils.UserUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 请假接口
 */
@RestController
@RequestMapping("/empLeavePermit")
public class EmpLeavePermitController {

    private static final Logger logger = LoggerFactory.getLogger(EmpLeavePermitController.class);

    @Autowired
    EmpLeavePermitService empLeavePermitService;

    /**
     * 请假列表
     *
     * @param request
     * @return
     */
    @GetMapping
    @RequiresPermissions("leavePermit:query")
    public PageTableResponse list(PageTableRequest request) {
        Map<String, Object> params = request.getParams();
        UserUtil.setParams(params);
        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return empLeavePermitService.count(params);
            }
        }, new ListHandler() {
            @Override
            public List<EmpLeavePermit> list(PageTableRequest request) {
                List<EmpLeavePermit> list = empLeavePermitService.list(params, request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    @PostMapping
    @RequiresPermissions("leavePermit:add")
    public ResponseInfo save(@RequestBody EmpLeavePermit param) {
        try {
            empLeavePermitService.save(param);
        } catch (Exception e) {
            logger.error("save Exception", e);
            return ResponseInfo.FAILURE.message("保存失败");
        }
        return ResponseInfo.SUCCESS;
    }

    @GetMapping("/{id}")
    @RequiresPermissions("leavePermit:query")
    public EmpLeavePermit findOne(@PathVariable Integer id) {
        return empLeavePermitService.findOne(id);
    }

    @PostMapping(value = "/aduit")
    @RequiresPermissions("leavePermit:query")
    public ResponseInfo aduit(@RequestBody EmpLeavePermit param) {
        try {
            empLeavePermitService.aduit(param);
        } catch (Exception e) {
            logger.error("save Exception", e);
            return ResponseInfo.FAILURE.message("审批失败");
        }
        return ResponseInfo.SUCCESS;
    }

}
